package com.jinbo.sxzq;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/************显示游戏进程的类**********************/
public class GameView extends SurfaceView implements SurfaceHolder.Callback{

    GameActivity gameActivity;
    GameViewDrawThread gameViewDrawThread;//绘制线程的引用
    GameControl gameControl;//游戏控制器
    final int BoardWidthForOne = 80;//一格棋盘的大小
    final int ChessInDeploy = 100;//在信息显示区域的棋子大小
    final int RectLength = 200;//信息条的长度
    Bitmap[] chess_bitmaps;//棋子的图片资源


    Paint paint_1;//画笔
    Paint paint_2;
    Paint paint_3;
    int board_width = ProjectConstants.BoardAttributes.WIDTH;//棋盘宽度
    int board_length = ProjectConstants.BoardAttributes.LENGTH;//棋盘长度
    int first_boardLocationX;//第一块棋盘的x坐标
    int first_boardLocationY;//第一块棋盘的y坐标


    public GameView(GameActivity gameActivity) {
        super(gameActivity);
        this.gameActivity = gameActivity;
        getHolder().addCallback(this);//实现Callback方法
        gameControl = gameActivity.gameControl;
        init();
        gameControl.StarGame();
        gameViewDrawThread = new GameViewDrawThread(this);
    }

    public void init(){
        paint_1 = new Paint();
        paint_1.setAntiAlias(true);//抗锯齿
        paint_1.setFilterBitmap(true);//滤波处理
        paint_2 = new Paint();
        paint_2.setAntiAlias(true);//抗锯齿
        paint_2.setFilterBitmap(true);//滤波处理
        paint_3 = new Paint();
        paint_3.setAntiAlias(true);//抗锯齿
        paint_3.setFilterBitmap(true);//滤波处理

        first_boardLocationX = 30;
        first_boardLocationY = 30;

        //初始化图片资源
        chess_bitmaps = gameActivity.chess_bitmaps;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制棋盘
        super.onDraw(canvas);
        paint_1.setColor(Color.WHITE);
        canvas.drawRect(25,25,(35+BoardWidthForOne*board_width),(35+BoardWidthForOne*board_width),paint_1);
        paint_1.setColor(Color.GRAY);
        paint_2.setColor(Color.BLACK);
        paint_3.setColor(Color.YELLOW);

        //绘制棋盘
        for(int i = 0 ; i < board_width ; i++){
            for(int j = 0 ; j < board_length ; j++){
                if((i+j)%2 == 0){
                    canvas.drawRect(first_boardLocationX+j*BoardWidthForOne,
                            first_boardLocationY+i*BoardWidthForOne,
                            first_boardLocationX+(j+1)*BoardWidthForOne,
                            first_boardLocationY+(i+1)*BoardWidthForOne,paint_1);
                }else{
                    canvas.drawRect(first_boardLocationX+j*BoardWidthForOne,
                            first_boardLocationY+i*BoardWidthForOne,
                            first_boardLocationX+(j+1)*BoardWidthForOne,
                            first_boardLocationY+(i+1)*BoardWidthForOne,paint_2);
                }
                if(gameControl.getBoardChess(i,j) > 0){//判断绘制的棋子类型
                    Rect srcRect = new Rect(0,0,chess_bitmaps[0].getWidth(),chess_bitmaps[0].getHeight());
                    if(gameControl.getBoardOwner(i,j) == ProjectConstants.ChessOwner.OUR){//我方小边框
                        Rect destRect = new Rect(first_boardLocationX+j*BoardWidthForOne+5,
                                first_boardLocationY+i*BoardWidthForOne+5,
                                first_boardLocationX+(j+1)*BoardWidthForOne-5,
                                first_boardLocationY+(i+1)*BoardWidthForOne-5);
                        canvas.drawBitmap(chess_bitmaps[gameControl.getBoardChess(i,j)-1],
                                srcRect,
                                destRect,null);
                    }else{
                        Rect destRect = new Rect(first_boardLocationX+j*BoardWidthForOne+10,
                                first_boardLocationY+i*BoardWidthForOne+10,
                                first_boardLocationX+(j+1)*BoardWidthForOne-10,
                                first_boardLocationY+(i+1)*BoardWidthForOne-10);
                        canvas.drawBitmap(chess_bitmaps[gameControl.getBoardChess(i,j)-1],
                                srcRect,
                                destRect,null);
                    }
                }
            }
        }

        //绘制棋子信息区域(绘制棋子的生命值 能力值 状态)
        int first_location_X = 25;//第一个棋子信息的位置
        int first_location_Y = 1000;
        //绘制部署区域背景
        canvas.drawRect(first_location_X - 50,
                first_location_Y -50,
                first_location_X + 3 * (ChessInDeploy + 50 + RectLength + 10),
                first_location_Y + 5 * (ChessInDeploy + 20), paint_1);
        paint_1.setColor(Color.RED);//绘制生命值
        paint_2.setColor(Color.BLUE);//绘制能力值
        paint_3.setColor(Color.YELLOW);//绘制空的条格
        paint_1.setTextSize(40);
        paint_2.setTextSize(40);
        int onwner_chess = 0;//我方棋子的数量
        //绘制信息区域
        /*
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){
            Chess chess = gameControl.chess_list.get(i);
            if(chess.owner == ProjectConstants.ChessOwner.OUR){
                //绘制棋子图片
                int move_X = (onwner_chess%3) * (ChessInDeploy + 50 + RectLength + 10);//在X轴的偏移量
                int move_Y = (onwner_chess/3) * (ChessInDeploy+20);//在Y轴的偏移量
                canvas.drawRect(first_location_X + move_X,
                        first_location_Y + move_Y,
                        first_location_X + ChessInDeploy + move_X,
                        first_location_Y+ChessInDeploy + move_Y,paint_3);
                //绘制空的条格

                canvas.drawRect(first_location_X + ChessInDeploy+50 + move_X,
                        first_location_Y + move_Y,
                        first_location_X + ChessInDeploy+50+RectLength + move_X,
                        first_location_Y+40 + move_Y,paint_3);
                canvas.drawRect(first_location_X+ChessInDeploy + 50 + move_X,
                        first_location_Y + 60 + move_Y,
                        first_location_X+ChessInDeploy+50+RectLength + move_X,
                        first_location_Y+100  + move_Y,paint_3);

                //绘制生命值和能力值
                canvas.drawRect(first_location_X+ChessInDeploy+50 + move_X,
                        first_location_Y + move_Y,
                        first_location_X+ChessInDeploy+50+RectLength * (chess.maxHp == 0 ? 0 : (chess.hp/chess.maxHp)) + move_X,//除以0问题
                        first_location_Y+40 + move_Y,paint_1);
                canvas.drawRect(first_location_X+ChessInDeploy+50 + move_X,
                        first_location_Y+60 + move_Y,
                        first_location_X+ChessInDeploy+50+RectLength * (chess.maxMp == 0 ? 0 : (chess.mp/chess.maxMp)) + move_X,
                        first_location_Y+100 + move_Y,paint_2);
                onwner_chess++;
            }
        }

         */ //图形条版
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){//文字信息版
            Chess chess = gameControl.chess_list.get(i);
            if(chess.owner == ProjectConstants.ChessOwner.OUR){
                //绘制棋子图片
                int move_X = (onwner_chess%3) * (ChessInDeploy + 50 + RectLength + 10);//在X轴的偏移量
                int move_Y = (onwner_chess/3) * (ChessInDeploy+20);//在Y轴的偏移量
                Rect srcRect = new Rect(0,0,chess_bitmaps[0].getWidth(),chess_bitmaps[0].getHeight());
                Rect destRect = new Rect(first_location_X + move_X,
                        first_location_Y + move_Y,
                        first_location_X + ChessInDeploy + move_X,
                        first_location_Y+ChessInDeploy + move_Y);
                canvas.drawBitmap(chess_bitmaps[chess.type-1],srcRect,destRect,null);
                //绘制生命值和能力值
                canvas.drawText("生命值：" + chess.hp ,
                        first_location_X + ChessInDeploy + 40 + move_X ,
                        first_location_Y + move_Y +30, paint_1);
                canvas.drawText("能力值：" + chess.mp ,
                        first_location_X + ChessInDeploy + 40 + move_X ,
                        first_location_Y + move_Y + 90, paint_2);
                onwner_chess++;
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        gameViewDrawThread.setFlag(true);
        gameViewDrawThread.start();//开启绘制线程
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        gameViewDrawThread.setFlag(false);//关闭绘制线程
        while (retry) {
            try {
                gameViewDrawThread.join();//阻塞线程
                retry = false;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
